<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  NotifyManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->
<cfcomponent extends="EventManager" output="false">

	<cffunction name="init" access="public" returntype="EventManager" output="false" hint="Constructor">
		
		<cfset variables.eventListener = structNew() />
		
		<cfreturn this />
	</cffunction>

	<cffunction name="configureEvent" access="public" returntype="void" output="false">
		<cfargument name="name" type="String" required="true" hint="Event name" />
    	<cfscript>
    	addEvent( arguments.name );
		</cfscript>
    </cffunction>
	
	<cffunction name="fireEvent" access="public" returntype="void">
		<cfargument name="name" type="String" required="true" hint="Event name" />
		<cfargument name="args" type="Struct" required="false" default="#structNew()#" hint="Event value" />
		<cfset var listeners = variables.eventListener[arguments.name].listeners.iterator() />
		<cfset var listener = structNew() />
		
		<cfloop condition="#listeners.hasNext()#">
		
			<cfset listener = listeners.next() />
			
			<cfset trace( arguments.name ) />
			
			<cfif listener.async >
			
				<cfthread name="#arguments.name#_#createUUID()#" action="run" component="#listener.object#" method="#arguments.name#" argumentcollection="#arguments.args#" >
					
					<cfinvoke component="#component#" method="onNotify" argumentcollection="#argumentcollection#" />
			
				</cfthread>
				
			<cfelse>
			
				<cfinvoke component="#listener.object#" method="onNotify" argumentcollection="#arguments.args#" />
			
			</cfif>
			
		</cfloop>
		
		<cfset incr( arguments.name ) />
		
	</cffunction>

</cfcomponent>